package com.shxy.rlzy.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.shxy.rlzy.common.lang.Result;
import com.shxy.rlzy.entity.Menu;
import com.shxy.rlzy.service.IMenuService;
import com.shxy.rlzy.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: tanhao
 * @Description: 测试控制器
 * @DateTime: 8/16/2021 12:27 PM
 **/
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    IUserService userService;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    IMenuService menuService;

    @GetMapping("/")
    public Result test() {
        return Result.succ(userService.list());
    }

    @GetMapping("/pass")
    public Result pass() {
        String pass = bCryptPasswordEncoder.encode("111111");
        return Result.succ(pass);
    }

    /**
     * 获取菜单
     * @return
     */
    @GetMapping("/sys/menu/nav")
    public Result getNav() {
        JSONObject jc1 = JSONUtil.createObj();
        jc1.set("title", "用户管理");
        jc1.set("name", "UserManage");
        jc1.set("icon", "el-icon-user-solid");
        jc1.set("component", "sys/Users");
        jc1.set("path", "/sys/users");

        JSONObject jc2 = JSONUtil.createObj();
        jc2.set("title", "角色管理");
        jc2.set("name", "RoleManage");
        jc2.set("icon", "el-icon-rank");
        jc2.set("component", "sys/Roles");
        jc2.set("path", "/sys/roles");

        JSONObject jc3 = JSONUtil.createObj();
        jc3.set("title", "菜单管理");
        jc3.set("name", "MenuManage");
        jc3.set("icon", "el-icon-menu");
        jc3.set("component", "sys/Menus");
        jc3.set("path", "/sys/menus");

        JSONObject jp1 = JSONUtil.createObj();
        jp1.set("title", "系统管理");
        jp1.set("name", "SysManage");
        jp1.set("icon", "el-icon-s-operation");
        jp1.set("component", "Home");
        jp1.set("path", "");
        jp1.set("children", Arrays.asList(jc1, jc2, jc3));

        JSONObject jc4 = JSONUtil.createObj();
        jc4.set("title", "数据字典");
        jc4.set("name", "DataDic");
        jc4.set("icon", "el-icon-notebook-2");
        jc4.set("component", "sys/Dicts");
        jc4.set("path", "/sys/dicts");

        JSONObject jp2 = JSONUtil.createObj();
        jp2.set("title", "系统工具");
        jp2.set("name", "SysTool");
        jp2.set("icon", "el-icon-setting");
        jp2.set("component", "Home");
        jp2.set("path", "");
        jp2.set("children", Arrays.asList(jc4));

//        JSONObject rs = JSONUtil.createObj();
//        rs.set("nav", Arrays.asList(jp1, jp2));
//        rs.set("authorities", new ArrayList<>());

        return Result.succ(Arrays.asList(jp1, jp2));
    }

    @GetMapping("/sys/menu/nav1")
    public Result getMenus() {
        List<Menu> menus = menuService.getNavsByAdminId(1L);
        return Result.succ(menus);
    }

    @GetMapping("/static/path")
    public String getStaticPath() {
        return ClassUtils.getDefaultClassLoader().getResource("static").getPath();
    }
}
